package kr.saeildadim.androidexam_saeildamil.chapter17;

import kr.saeildadim.androidexam_saeildamil.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class Ch17_Common extends Activity {
	final static int ADD=0;
	final static int SUB=1;
	final static int MUL=2;
	
	EditText l_Value;
	EditText r_Value;
	EditText result;
	EditText oper_Value;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.ch17_common);
		l_Value = (EditText) findViewById(R.id.ch17_common_ed1);
		r_Value = (EditText) findViewById(R.id.ch17_common_ed3);
		oper_Value = (EditText) findViewById(R.id.ch17_common_ed2);
		result = (EditText) findViewById(R.id.ch17_common_ed4);
	}
	public void mOnClick(View v){
		if(v.getId()==R.id.ch17_common_btn){
			Intent intent = new Intent();
				intent.putExtra("lValue", l_Value.getText().toString());
				String opr = oper_Value.getText().toString().trim();
				intent.putExtra("oper", opr);
				intent.putExtra("rValue", r_Value.getText().toString());
		
				if(opr.equals("+")){
					intent.setClass(this, AddAct.class);
					startActivityForResult(intent, ADD);
				}else if(opr.equals("-")){
					intent.setClass(this, SubAct.class);
					startActivityForResult(intent, SUB);
				}else{
					intent.setClass(this, MulAct.class);
					startActivityForResult(intent, MUL);
				}
		}
	}//mON end
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
			
		switch (requestCode){
			case ADD:
				if(resultCode==RESULT_OK){
					Toast.makeText(this, "ADD결과완료", Toast.LENGTH_LONG).show();
					result.setText(data.getIntExtra("AddResult", 0)+"");
				}else{
					Toast.makeText(this, "ADD결과실패", Toast.LENGTH_LONG).show();
				}
				break;
			case SUB:
				if(resultCode==RESULT_OK){
					Toast.makeText(this, "Sub결과완료", Toast.LENGTH_LONG).show();
					result.setText(data.getIntExtra("SubResult", 0)+"");
				}else{
					Toast.makeText(this, "Sub결과실패", Toast.LENGTH_LONG).show();
				}
				break;
			case MUL:
				if(resultCode==RESULT_OK){
					Toast.makeText(this, "Mul결과완료", Toast.LENGTH_LONG).show();
					result.setText(data.getIntExtra("MulResult", 0)+"");
				}else{
					Toast.makeText(this, "Mul결과실패", Toast.LENGTH_LONG).show();
				}

				break;
		}
	}
	
	
}
